package net.mikoo.seals.biz.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import net.mikoo.seals.biz.common.BaseServices;
import net.mikoo.seals.biz.mapper.OrganizationMapper;
import net.mikoo.seals.biz.mapper.StaffMapper;
import net.mikoo.seals.biz.service.StaffServices;
import net.mikoo.seals.common.enums.StaffStatusEnum;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.model.OrganizationParty;
import net.mikoo.seals.model.Staff;
import net.mikoo.seals.model.StaffQuery;
import net.mikoo.seals.utils.ValidatorUtil;

public class StaffServicesImpl extends BaseServices implements StaffServices {

	@Autowired
	private StaffMapper staffMapper;
	
	@Autowired
	private OrganizationMapper organizationMapper;
	
	@Override
	public Integer getTotal(Long companyId) {
		
		Integer total = staffMapper.countTotal(companyId);
		
		return total;
	}

	@Override
	public Staff getInfoByQuery(StaffQuery query) {
		Staff staff = staffMapper.selectInfoByQuery(query);
		return staff;
	}

	@Override
	public void changeStatus(Staff staff) {
		if (staff == null) {
			throw new ParametersException("staff is null!");
		}
		if (staff.getPartyId() == null) {
			throw new ParametersException("PartyId is null!");
		}
		if (staff.getCompanyId() == null) {
			throw new ParametersException("CompanyId is null!");
		}
		
		if (StringUtils.isBlank(staff.getStaffStatus())) {
			throw new ParametersException("StaffStatus is Blank!");
		}
		
		StaffStatusEnum staffStatusEnum = StaffStatusEnum.getByCode(staff.getStaffStatus());
		if(staffStatusEnum == null) {
			throw new ParametersException("StaffStatus is unkonw");
		}
		
		
		if(StringUtils.isNotBlank(staff.getEmail())) {
			if(!ValidatorUtil.isEmail(staff.getEmail())) {
				throw new ParametersException("邮箱格式输如有误！");
			}
		}
		
		staffMapper.update(staff);
		
	}

	@Override
	public void modify(Staff staff) {
		
		if (staff == null) {
			throw new ParametersException("staff is null!");
		}
		if (staff.getPartyId() == null) {
			throw new ParametersException("PartyId is null!");
		}
		if (staff.getCompanyId() == null) {
			throw new ParametersException("CompanyId is null!");
		}
		Long orgId = staff.getOrgId();
		
		
		this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				try {
					
					if(orgId !=null) {
						OrganizationParty op = new OrganizationParty();
						op.setCompanyId(staff.getCompanyId());
						op.setPartyId(staff.getPartyId());
						op.setOrganizationId(orgId);
						
						organizationMapper.deleteParty(op);
						
						organizationMapper.insertParty(op);
					}
					
					staffMapper.update(staff);
					
				}catch (Exception e) {
					status.setRollbackOnly();
					throw e;
				}
				
			}
		});
		
		
		
	}

}
